//
//  WJMainTableView.m
//  WJBSApp
//
//  Created by rumble on 2018/11/27.
//  Copyright © 2018年 王景伟. All rights reserved.
//

#import "WJMainTableView.h"

#import "WJEssAndNewPLVC.h"
#import "WJShowPictureVC.h"
#import "WJPhotoBrowser.h"
#import "WJShowHtmlVC.h"
#import "WJFowardVC.h"
#import "WJLastViewController.h"
#import "WJ_TJDY_VC.h"

@interface WJMainTableView () <UITableViewDelegate,UITableViewDataSource,WJMainTableViewCellDelegate>
@property (nonatomic, strong) UIViewController *controller;
@property (nonatomic, strong) NSIndexPath *playPath;
@end

@implementation WJMainTableView

+ (instancetype)mainTableViewWithFrame:(CGRect)frame controller:(UIViewController *)controller {
    WJMainTableView *tableView = [[WJMainTableView alloc]initWithFrame:frame style:UITableViewStylePlain];
    tableView.controller = controller;
    return tableView;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        if (@available(iOS 11.0, *)) {
            self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[WJMainTableViewCell class] forCellReuseIdentifier:@"WJMainTableViewCell"];
        [self registerClass:[WJMainImageCell class] forCellReuseIdentifier:@"WJMainImageCell"];
        [self registerClass:[WJMainVideoCell class] forCellReuseIdentifier:@"WJMainVideoCell"];
        [self registerClass:[WJMianAudioCell class] forCellReuseIdentifier:@"WJMianAudioCell"];
        self.dataArr = [NSMutableArray array];
        self.headerEnabled = YES;
        self.tagEnabled = YES;
    }
    return self;
}

- (void)setDataArr:(NSMutableArray<WJMianLayout *> *)dataArr {
    _dataArr = dataArr;
    [self reloadData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WJMianLayout *layout = self.dataArr[indexPath.row];
    return layout.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WJMianLayout *layout = self.dataArr[indexPath.row];
    layout.indexPath = indexPath;
    
    if (layout.style == WJMianLayoutImage || layout.style == WJMianLayoutGif || layout.style == WJMianLayoutHtml) {
        WJMainImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainImageCell" forIndexPath:indexPath];
        cell.layout = layout;
        cell.cellDelegate = self;
        return cell;
    } else if (layout.style == WJMianLayoutVideo) {
        WJMainVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainVideoCell" forIndexPath:indexPath];
        cell.layout = layout;
        cell.cellDelegate = self;
        return cell;
    } else if (layout.style == WJMianLayoutAudio) {
        WJMianAudioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMianAudioCell" forIndexPath:indexPath];
        cell.layout = layout;
        cell.cellDelegate = self;
        return cell;
    } else {
        WJMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainTableViewCell" forIndexPath:indexPath];
        cell.layout = layout;
        cell.cellDelegate = self;
        return cell;
    }
}

#pragma mark - cell 点击代理
// 点击视频 点击音频的时候 直接播放。点击图片查看大图的时候我们需要查看大图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WJMianLayout *layout = self.dataArr[indexPath.row];
    [self pushPingLunWithLayout:layout];
}

///> ispush 就跳转看详情。 不是就查看大图
- (void)mainTableViewCellClickImageWithLayout:(WJMianLayout *)layout imageView:(UIImageView *)imageView isPush:(BOOL)ispush {
    if (ispush) {
        [self pushPingLunWithLayout:layout];
    } else {
        [self pushImageControllerWithLayout:layout imageView:imageView];
    }
}
- (void)mainTableViewCellClickGifWithLayout:(WJMianLayout *)layout {
    [self pushPingLunWithLayout:layout];
}
///> 跳转H5
- (void)mainTableViewCellClickHtmlWithLayout:(WJMianLayout *)layout {
    [self pushHtmlWithLayout:layout];
}
- (void)mainTableViewCellClickVideoWithLayout:(WJMianLayout *)layout {
    self.playPath = layout.indexPath;
}
- (void)mainTableViewCellClickAudioWithLayout:(WJMianLayout *)layout {
    
}

- (void)pushPingLunWithLayout:(WJMianLayout *)layout {
    WJEssAndNewPLVC *plvc = WJEssAndNewPLVC.new;
    plvc.layout = layout;
    [_controller.navigationController pushViewController:plvc animated:YES];
}

- (void)pushImageControllerWithLayout:(WJMianLayout *)layout imageView:(UIImageView *)imageView {
//    WJShowPictureVC *imgvc = WJShowPictureVC.new;
//    imgvc.model = layout.model;
//    [_controller.navigationController pushViewController:imgvc animated:YES];
    
    WJPhotoBrowserModel *model = WJPhotoBrowserModel.new;
    model.thumbView = imageView;
    model.largeImageURL = [NSURL URLWithString:layout.model.image.big[0]];
    model.largeImageSize = CGSizeMake(layout.model.image.width, layout.model.image.height);
    
    WJPhotoBrowser * brower = [[WJPhotoBrowser alloc]initWithGroupModel:@[model]];
    [brower presentFromImageView:imageView toContainer:[UIApplication sharedApplication].keyWindow animated:YES completion:^{}];
}

- (void)pushHtmlWithLayout:(WJMianLayout *)layout {
    WJShowHtmlVC *htmlvc = WJShowHtmlVC.new;
    htmlvc.model = layout.model;
    [_controller.navigationController pushViewController:htmlvc animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect cellrect =  [self rectForRowAtIndexPath:self.playPath];
    if (fabs(self.contentOffset.y - cellrect.origin.y) > self.size.height ) {
        [[WJVideoPlayView sharedPlayView] resetAVPlayer];
    }
    if (self.scrollDelegate && [self.scrollDelegate respondsToSelector:@selector(mainTableViewDidScroll:)]) {
        [self.scrollDelegate mainTableViewDidScroll:self];
    }
}

- (void)mainTableViewCellClickHeaderWithLayout:(WJMianLayout *)layout {
    if (!_headerEnabled) return;
    WJLastViewController *vc = [[WJLastViewController alloc]init];
    vc.userID = layout.model.user.uid;
    vc.userName = layout.model.user.name;
    [_controller.navigationController pushViewController:vc animated:YES];
}
- (void)mainTableViewCellClickCommentWithLayout:(WJMianLayout *)layout {
    [self pushPingLunWithLayout:layout];
}
- (void)mainTableViewCellClickFowardWithLayout:(WJMianLayout *)layout {
    WJFowardVC *vc = WJFowardVC.new;
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.6];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [BSRootViewController presentViewController:vc animated:YES completion:nil];
}
- (void)mainTableViewCellClickTagWithModel:(Tag *)model {
    if (!_tagEnabled) return;
    WJ_TJDY_VC *vc = WJ_TJDY_VC.new;
    vc.theme_id = model.idStr;
    vc.title = model.name;
    [_controller.navigationController pushViewController:vc animated:YES];
}

@end
